// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "Windows.h"
#include "stdlib.h"
#include "stdio.h"

static char * g_pBuff = 0;


extern "C"  void __stdcall HookFun()
{
	MessageBox(NULL, "Starting to intall hook.", "hook", MB_OK);
	__asm 
	{
		mov ax , ip
	}
}


BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		{
			MessageBox(NULL, "Starting to read memory data...", "Succ", MB_OK);
			char * g_pBuff = (char *)malloc(32*32);
			char * pBaseAddr = (char *)(0x1005340+0x20+0x1);

			for (unsigned int i=0, size=32*32; i<size; ++i)
			{
				g_pBuff[i] = pBaseAddr[i];	
			}

			//FILE * pFile = 0;
			//errno_t err = fopen_s(&pFile, "C:/tmp.txt", "w+");

			//if (0 != err)
			//	return -1;

			//for (unsigned int i=0, size=32*32; i<size; ++i)
			//{
			//	//fputc(pBaseAddr[i], pFile);
			//	if (0 == (i%32) && i!=0)
			//		fputc('\n', pFile);					
			//	fprintf(pFile, "%4d ", pBaseAddr[i]);
			//}

			LPVOID si = (LPVOID)0x01003597;
			LPVOID di = (LPVOID)HookFun;

			BYTE code[5];
			DWORD offset = (DWORD)di - ((DWORD)si+5);

			code[0]=0xe9;
			*PDWORD(code+1) = offset;

			DWORD oldprotect;
			if(VirtualProtect(si,5,PAGE_EXECUTE_READWRITE,&oldprotect))
			{
				memcpy(si,code,5);
				VirtualProtect(si,5,oldprotect,&oldprotect);
			}

			//fclose(pFile);

			//delete pBuff;
			//pBuff = 0;
		}
		break;
	case DLL_THREAD_ATTACH:
		MessageBox(NULL, "DLL_THREAD_ATTACH.", "Abc", MB_OK);
		break;
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}

